package cn.gson.crm.controller;

import cn.gson.crm.model.mapper.CenterMapper;
import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.service.SalesService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/sales")
public class SalesController{

    @Autowired
    SalesService salesService;
    @Autowired
    CenterMapper centerMapper;
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=centerMapper.selectUserRole(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return 0;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }

    @RequestMapping("/productSelect")
    @ResponseBody
    public List<ProductXq> productSelect(){
        return salesService.productSelect1();
    }

    //sales:查询购物车产品
    @RequestMapping("/selectCart")
    @ResponseBody
    public List<Cart> selectCart(int salesId){
        return salesService.selectCart(salesId);
    }

    //sales:删除销售机会
    @RequestMapping("/deleteSales")
    @ResponseBody
    public String deleteSales(int salesId){
        return salesService.deleteSales(salesId);
    }

    //sales:查询销售机会
    @RequestMapping("/salesSelect")
    @ResponseBody
    public Map<String, Object> salesSelect(Integer pageNum,Integer pageSize,String salesTheme, String salesStage, HttpSession session){
        int ygId;
        ygId = fouyuan(session);
        List<Sales> sales = salesService.selectSales(ygId,0, salesTheme, salesStage);
        Page<Object> page = PageHelper.startPage(pageNum,pageSize);
        salesService.selectSales(ygId, 0, salesTheme, salesStage);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        map.put("sales",sales);
        //订单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String code="XS"+sdf.format(new Date());
        map.put("code",code);
        return map;
    }

    //sales:新增销售机会
    @RequestMapping("/addSales")
    @ResponseBody
    public void addSales(String position){
        salesService.addSales(position);
    }

    //option:查询销售机会
    @RequestMapping("/selectSales")
    @ResponseBody
    public List<Sales> selectSales(int salesId,HttpSession session){
        return salesService.selectSales(0,salesId,null,null);
    }

    //sales:查询产品
    @RequestMapping("/product")
    @ResponseBody
    public List<Stock> select(String categoryId,String cpName){
        if(categoryId==null || categoryId==""){
            categoryId="0";
        }
        long id = Long.parseLong(categoryId);
        return salesService.productSelect(id,cpName);
    }

    //option:客户需求修改
    @RequestMapping("/khxqUpdate")
    @ResponseBody
    public void khxqUpdate(String salesId,String salesDemand,String salesRemark){
        salesService.khxqUpdate(salesId,salesDemand,salesRemark);
    }

    //option:solution:查询解决方案
    @RequestMapping("/selectSolution")
    @ResponseBody
    public List<Solution> selectSolution(int salesId){
        return salesService.selectSolution(salesId);
    }

    //option:solution:新增修改
    @RequestMapping("/addSolution")
    @ResponseBody
    public void addSolution(@RequestBody Solution solution){
        solution.setSolutionState("1");
        salesService.addSolution(solution);
    }

    //option:solution:删除解决方案
    @RequestMapping("/updateSolution")
    @ResponseBody
    public void updateSolution(int solutionId){
        salesService.updateSolution("0",solutionId);
    }

    //competitor:查询竞争对手
    @RequestMapping("/selectCompetitor")
    @ResponseBody
    public Map<String,Object> selectCompetitor(Integer pageNum,Integer pageSize,int salesId,String compeCompany){
        Page<Object> page = PageHelper.startPage(pageNum,pageSize);
        salesService.selectCompetitor(salesId,compeCompany);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        return map;
    }

    //competitor:删除竞争对手
    @RequestMapping("/updateCompetitor")
    @ResponseBody
    public void updateCompetitor(int compeId){
        salesService.updateCompetitor(compeId);
    }

    //competitor:新增修改竞争对手
    @RequestMapping("/addCompetitor")
    @ResponseBody
    public void addCompetitor(@RequestBody Competitor competitor){
        competitor.setCompeState("1");
        salesService.addCompetitor(competitor);
    }

    //offer:新增修改报价
    @RequestMapping("/addOffer")
    @ResponseBody
    public void addOffer(String position,HttpSession session){
        User login = (User)session.getAttribute("login");
        long ygId=login.getEmployees().get(0).getYgId();
        salesService.addOffer(position,ygId);
    }

    //offer:查询报价，报价详单，购物车
    @RequestMapping("/selectOffer")
    @ResponseBody
    public List<Offer> selectOffer(int salesId){
        return salesService.selectOffer(salesId);
    }

    //offer:删除报价
    @RequestMapping("/updateOffer")
    @ResponseBody
    public void updateOffer(int offerId){
        salesService.updateOffer(offerId);
    }

    //funnel:漏斗图
    @RequestMapping("/create")
    @ResponseBody
    public Map<String,Object> create(String leader,HttpSession session){
        int ygId;
        ygId=fouyuan(session);
        return salesService.create(leader,ygId);
    }

    //option:follow:新增修改
    @RequestMapping("/addFollow")
    @ResponseBody
    public void addFollow(@RequestBody Follow follow){
        salesService.addFollow(follow);
    }

    //option:follow:查询跟进记录
    @RequestMapping("/selectFollow")
    @ResponseBody
    public Map<String,Object> selectFollow(Integer pageNum,Integer pageSize,int salesId){
        Page<Object> page = PageHelper.startPage(pageNum,pageSize);
        salesService.selectFollow(salesId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        return map;
    }

    //option:follow:删除跟进记录
    @RequestMapping("/deleteFollow")
    @ResponseBody
    public void deleteFollow(int followId){
        salesService.deleteFollow(followId);
    }

    //print：报价查询
    @RequestMapping("/printOffer")
    @ResponseBody
    public List<Offer> printOffer(int offerId){
        return salesService.printOffer(offerId);
    };


    //主页销售漏斗
    @RequestMapping("/select1")
    @ResponseBody
    public Map<String,Object> select1(String orderTime){
        Map<String,Object> map=null;
        if(orderTime.equals("2020")){
           map=salesService.select1();
        }
        if(orderTime.equals("2019")){
            map=salesService.select2();
        }
        else {
            map=salesService.select1();
        }
        return map;
    }
    @RequestMapping("/selectsales")
    @ResponseBody
    public List<Shuliang> selectsales(){
        return salesService.selectsales();
    }
    //print：报价详情查询
    @RequestMapping("/offerDetailPrint")
    @ResponseBody
    public List<OfferDetail> offerDetailPrint(int offerId){
        return salesService.offerDetailPrint(offerId);
    };
}
